package com.sinven.controller.manage;

import com.sinven.common.ActionResult;
import com.sinven.pojo.Environment;
import com.sinven.service.EnvService;
import com.sinven.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("manage")
public class EnvController {
    @Autowired
    private EnvService envService;

    @GetMapping("envs")
    public ActionResult<List<Environment>> queryEnv(@RequestParam(value = "curPage", defaultValue = "1") Integer curPage,
                                                    @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize){
        return envService.queryCurPageData(curPage,pageSize);
    }

    @GetMapping("envs/{eid}")
    public ActionResult<Environment> getEnv(@PathVariable("eid")long eid){
        return ResultUtil.success(envService.getEnvById(eid));
    }

    @PostMapping("envs")
    public ActionResult addEnv(Environment env){
        boolean result = envService.addEnv(env);
        if (result) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(500, "服务器错误");
        }
    }
    @PutMapping("envs")
    public ActionResult updateEnv(Environment env){
        boolean result = envService.updateEnv(env);
        if (result) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(500, "服务器错误");
        }
    }
    @DeleteMapping("envs")
    public ActionResult deleteEnv(long eid){
        boolean result = envService.deleteEnv(eid);
        if (result) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(500, "服务器错误");
        }
    }
}
